package com.business.controller.interfaced;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.business.service.interfaced.BusinessInManager;
import com.business.service.interfaced.ProColorSizeInManager;
import com.business.service.interfaced.ProductInManager;
import com.business.service.interfaced.StandardInManager;
import com.google.gson.Gson;
import com.honour.service.interfaced.ShippingInManager;
import com.honour.service.interfaced.SortInManager;
import com.honour.service.interfaced.impl.AdvertisementInService;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.Const;
import com.system.util.FTPUtils;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.ToolsUtil;
import com.system.util.UpdImgUtils;

import net.sf.json.JSONObject;

/**
 * 商品接口
 * @Function 
 * @Author zhaozhenhua
 * @Date 2018年1月26日
 */
@Controller
@RequestMapping(value="product")
public class ProductInController extends BaseController{
	
	@Resource(name="shipinService")
	private ShippingInManager shipinService;
	
	@Resource(name="sortinService")
	private SortInManager sortinService;
	
	@Resource(name="advertisementInService")
	private AdvertisementInService advertisementInService;
	
	@Resource(name="productInService")
	private ProductInManager productInService;
	
	@Resource(name="standardInService")
	private StandardInManager standardInService;
	
	@Resource(name="procolorsizeInService")
	private ProColorSizeInManager procolorsizeInService;
	
	@Resource(name="businessInService")
	private BusinessInManager businessInService;
	
	Gson gson = new Gson();
	
	//商品实物图片地址
	private static String PRODUCTIMG="";
	
	//商品详情图片地址
	private static String PRODETAILSIMG="";
	
	
	/**
	 * 添加商品
	 * @Author zhaozhenhua
	 * @date   2018年1月26日
	 * @return
	 */
	@RequestMapping(value="/addproduct")
	@ResponseBody
	public Object addProduct(HttpServletResponse resp,HttpServletRequest request){
		logBefore(logger, "添加商品");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("ADDPRODUCT", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addProduct", pd)){	//检查参数
					//发布时间
					Date date = new Date();
					String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
					pd.put("CREATE_TIME", format);
					pd.put("PRODUCT_ID", this.getSnowId());
					pd.put("PRODUCTIMG", PRODUCTIMG.substring(1));
					pd.put("PRODETAILSIMG", PRODETAILSIMG.substring(1));
					PageData business = businessInService.getBusinessInfo(pd);
					pd.put("BRANDICON_BRANDID", business.get("BRANDICON_ID"));
					productInService.addProduct(pd);
					PRODUCTIMG="";
					PRODETAILSIMG="";
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 添加商品轮播图
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年1月31日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/upproductimg")
	public Object upProductImg(MultipartHttpServletRequest mreq,HttpServletResponse resp,HttpServletRequest request){
		logBefore(logger, "添加商品轮播图");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("UPPRODUCTIMG", pd.getString("FKEY"))){	//检验请求key值是否合法
			        	String filePath = "";
						  Iterator<String> iterator = mreq.getFileNames();
						    while (iterator.hasNext()) { 
						    	MultipartFile file = mreq.getFile(iterator.next());
						    	 String imgname = UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
					            	InputStream in = file.getInputStream();
										resp.setCharacterEncoding("UTF-8");
										resp.getWriter().write(filePath+";");
										//根据txt文件读取ftp工具类上传需要的参数
										String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
										String[] split = updeploy.split(",");
										//第一个参数为  图片服务器的地址
										//第二个参数为  图片服务器接口
										//第三/四个参数为  FTP服务器的登录用户名和密码
										//通过工具类获取图片服务器地址
										String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
										//String path="productimgs/productimg";
										String path="test";
										FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3],path, imgname, in);
										PRODUCTIMG+=","+imgpath+path+"/"+imgname;
								}
						    System.out.println("添加商品轮播图PRODUCTIMG:"+PRODUCTIMG);
						    result = "01";
					result = "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 添加商品详情图
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年1月31日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/upproductdetimg")
	public Object upProductDetImg(MultipartHttpServletRequest mreq,HttpServletResponse resp,HttpServletRequest request){
		logBefore(logger, "添加商品详情图");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("UPPRODUCTDETIMG", pd.getString("FKEY"))){	//检验请求key值是否合法
					String filePath = "";
					  Iterator<String> iterator = mreq.getFileNames();
					    while (iterator.hasNext()) { 
					    	MultipartFile file = mreq.getFile(iterator.next());
					    	 String imgname = UpdImgUtils.getUniqueFileName(file.getOriginalFilename());
				            	InputStream in = file.getInputStream();
									resp.setCharacterEncoding("UTF-8");
									resp.getWriter().write(filePath+";");
									//根据txt文件读取ftp工具类上传需要的参数
									String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
									String[] split = updeploy.split(",");
									//第一个参数为  图片服务器的地址
									//第二个参数为  图片服务器接口
									//第三/四个参数为  FTP服务器的登录用户名和密码
									//通过工具类获取图片服务器地址
									String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
									//String path="productimgs/productimg/productdetimgs";
									String path="test";
									FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3],path, imgname, in);
									PRODETAILSIMG+=","+imgpath+path+"/"+imgname;
							}
					    System.out.println("添加商品详情图PRODETAILSIMG:"+PRODETAILSIMG);
					    result = "01";
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 修改商品
	 * @Author zhaozhenhua
	 * @date   2018年1月31日
	 * @return
	 */
	@RequestMapping(value="/updproduct")
	@ResponseBody
	public Object updProduct(){
		logBefore(logger, "添加商品");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("UPDPRODUCT", pd.getString("FKEY"))){	//检验请求key值是否合法
				//if(AppUtil.checkParam("addProduct", pd)){	//检查参数
					PageData p = productInService.findById(pd);
					if(pd.getString("PRODUCTIMG")!=null&&pd.getString("PRODUCTIMG")!=""){
						UpdImgUtils.updImgsMethods(p, pd, "PRODUCTIMG",PRODUCTIMG.substring(1),"productimgs/productimg");
					}if(pd.getString("PRODETAILSIMG")!=null&&pd.getString("PRODETAILSIMG")!=""){
						UpdImgUtils.updImgsMethods(p, pd, "PRODETAILSIMG", PRODETAILSIMG.substring(1), "productimgs/prodetimgs");
					}
					productInService.updProduct(pd);
					PRODUCTIMG="";
					PRODETAILSIMG="";
					result = (null == pd) ?  "02" :  "01";
				/*}else {
					result = "03";
				}*/
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 删除商品
	 * @Author zhaozhenhua
	 * @date   2018年1月31日
	 * @return
	 */
	@RequestMapping(value="/deleteproduct")
	@ResponseBody
	public Object deleteProduct(){
		logBefore(logger, "删除商品");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("DELPRODUCT", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipDetails", pd)){	//检查参数
					//调用工具类删除图片服务器上的旧图片
					PageData p = shipinService.getShipDetailsById(pd);
					UpdImgUtils.delImgsMethod(p,"PRODUCTIMG", "PRODETAILSIMG", "productimgs/productimg", "productimgs/productimg/productdetimgs");
					productInService.deleteProduct(p);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据商品id获取商品信息
	 * @Author zhaozhenhua
	 * @date   2018年2月2日
	 * @return
	 */
	@RequestMapping(value="/getproductinfo")
	@ResponseBody
	public Object getProductInfo(){
		logBefore(logger, "根据商品id获取商品信息");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETPRODUCTINFO", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipDetails", pd)){	//检查参数
					 pd = productInService.findById(pd);
					 ToolsUtil.putImgstoArray(pd, "PRODETAILSIMG");
					ToolsUtil.putImgstoArray(pd, "PRODUCTIMG");
					String [] arr = pd.getString("PROPROPERTY").split(",");
					if(pd.getString("PROPROPERTY").equals("暂无")||pd.getString("PROPROPERTY")==null||pd.getString("PROPROPERTY")==""){
						pd.put("PROPROPERTY", "暂无");
					}else{
						Map<String, String> m = new HashMap<String, String>();
						for(String s:arr){
							String[] ms = s.split(":");
							m.put(ms[0], ms[1]);
						}
						JSONObject PROPROPERTY = JSONObject.fromObject(m);
						pd.put("PROPROPERTY", PROPROPERTY);
					}
					map.put("pd", pd);
				//返回01为添加成功并返回
				result = (null==pd)?"02": "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据商家id获取后台商品销售排名 
	 * @Author zhaozhenhua
	 * @date   2018年1月31日
	 * @return
	 */
	@RequestMapping(value="/getproductsales")
	@ResponseBody
	public Object getProductSales(){
		logBefore(logger, "根据商家id获取后台商品销售排名");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETPRODUCTSALES", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getCouponListById", pd)){	//检查参数
					List<PageData> sales = productInService.getProductSales(pd);
					if (sales!=null) {
						map.put("sales", sales);
					}
				//返回01为添加成功并返回
				result =  (0 == sales.size())? "02":"01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 给商品添加规格
	 * @Author zhaozhenhua
	 * @date   2018年1月26日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/addstandard")
	@ResponseBody
	public Object addStandard(){
		logBefore(logger, "给商品添加规格");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("ADDSTANDARD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addStandard", pd)){	//检查参数
					String PRODUCT_ID = pd.getString("PRODUCT_ID");
					pd.put("STANDARD_TYPE", "1");
					PageData size = procolorsizeInService.listAll(pd);
					if(size==null){
						if(pd.getString("SIZE_ARRAY")!=null&&pd.getString("SIZE_ARRAY")!=""){
							PageData SIZE_PD = new PageData();
							String SIZE_ARRAY = pd.getString("SIZE_ARRAY");	
							SIZE_PD.put("PROCOLORSIZE_ID", this.getSnowId());
							SIZE_PD.put("NAME", SIZE_ARRAY);
							SIZE_PD.put("STANDARD_TYPE", "1");
							SIZE_PD.put("PRODUCT_ID", PRODUCT_ID);
							procolorsizeInService.save(SIZE_PD);
						}if (pd.getString("COLOR_ARRAY")!=null&&pd.getString("COLOR_ARRAY")!="") {
							PageData SIZE_COLOR = new PageData();
							String COLOR_ARRAY = pd.getString("COLOR_ARRAY");	
							SIZE_COLOR.put("PROCOLORSIZE_ID", this.getSnowId());
							SIZE_COLOR.put("NAME", COLOR_ARRAY);
							SIZE_COLOR.put("STANDARD_TYPE", "2");
							SIZE_COLOR.put("PRODUCT_ID", PRODUCT_ID);
							procolorsizeInService.save(SIZE_COLOR);
						}if (pd.getString("STANDARD_ARRAY")!=null&&pd.getString("STANDARD_ARRAY")!="") {
							String json = pd.getString("STANDARD_ARRAY");
							PageData p1 = gson.fromJson(json, PageData.class);
							List<PageData> STANDARD_ARRAY = (List<PageData>) p1.get("STANDARD_ARRAY");
							String json2 = gson.toJson(STANDARD_ARRAY);
							STANDARD_ARRAY = ToolsUtil.fromJson(json2, PageData.class);
							for (PageData p : STANDARD_ARRAY) {
								p.put("STANDARD_ID", this.getSnowId());
								p.put("PRODUCT_ID", PRODUCT_ID);
								standardInService.save(p);
							}
						}
						//返回01为添加成功并返回
						result =  "01";
					}else{
						pd.put("STANDARD_TYPE", "1");
						PageData SIZE_ARRAY = procolorsizeInService.listAll(pd);
						if(SIZE_ARRAY!=null){
							ToolsUtil.putImgstoArray(SIZE_ARRAY,"NAME");
							map.put("SIZE_ARRAY", SIZE_ARRAY);
						}
						
						PageData product = shipinService.getShipDetailsById(pd);
						if(product!=null){
							PageData p = new PageData();
							p.put("PROPRICE", product.get("PROPRICE"));
							p.put("STOCK", product.get("STOCK"));
							map.put("product", p);
						}
						pd.put("STANDARD_TYPE", "2");
						PageData COLOR_ARRAY = procolorsizeInService.listAll(pd);
						if(COLOR_ARRAY!=null){
							ToolsUtil.putImgstoArray(COLOR_ARRAY,"NAME");
							map.put("COLOR_ARRAY", COLOR_ARRAY);
						}
					}
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 删除商品规格
	 * @Author zhaozhenhua
	 * @date   2018年1月29日
	 * @return
	 */
	@RequestMapping(value="/deletestandard")
	@ResponseBody
	public Object deleteStandard(){
		logBefore(logger, "删除商品规格");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("DELSTANDARD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("deleteStandard", pd)){	//检查参数
				standardInService.delete(pd);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 修改商品规格
	 * @Author zhaozhenhua
	 * @date   2018年1月29日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/editstandard")
	@ResponseBody
	public Object editStandard(){
		logBefore(logger, "修改商品规格");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("EDITSTANDARD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addStandard", pd)){	//检查参数
					standardInService.delete(pd);
					procolorsizeInService.delete(pd);
					String PRODUCT_ID = pd.getString("PRODUCT_ID");
					pd.put("STANDARD_TYPE", "1");
					PageData size = procolorsizeInService.listAll(pd);
					if(size==null){
						if(pd.getString("SIZE_ARRAY")!=null&&pd.getString("SIZE_ARRAY")!=""){
							PageData SIZE_PD = new PageData();
							String SIZE_ARRAY = pd.getString("SIZE_ARRAY");	
							SIZE_PD.put("PROCOLORSIZE_ID", this.getSnowId());
							SIZE_PD.put("NAME", SIZE_ARRAY);
							SIZE_PD.put("STANDARD_TYPE", "1");
							SIZE_PD.put("PRODUCT_ID", PRODUCT_ID);
							procolorsizeInService.save(SIZE_PD);
						}if (pd.getString("COLOR_ARRAY")!=null&&pd.getString("COLOR_ARRAY")!="") {
							PageData SIZE_COLOR = new PageData();
							String COLOR_ARRAY = pd.getString("COLOR_ARRAY");	
							SIZE_COLOR.put("PROCOLORSIZE_ID", this.getSnowId());
							SIZE_COLOR.put("NAME", COLOR_ARRAY);
							SIZE_COLOR.put("STANDARD_TYPE", "2");
							SIZE_COLOR.put("PRODUCT_ID", PRODUCT_ID);
							procolorsizeInService.save(SIZE_COLOR);
						}if (pd.getString("STANDARD_ARRAY")!=null&&pd.getString("STANDARD_ARRAY")!="") {
							String json = pd.getString("STANDARD_ARRAY");
							PageData p1 = gson.fromJson(json, PageData.class);
							List<PageData> STANDARD_ARRAY = (List<PageData>) p1.get("STANDARD_ARRAY");
							String json2 = gson.toJson(STANDARD_ARRAY);
							STANDARD_ARRAY = ToolsUtil.fromJson(json2, PageData.class);
							for (PageData p : STANDARD_ARRAY) {
								p.put("STANDARD_ID", this.getSnowId());
								p.put("PRODUCT_ID", PRODUCT_ID);
								standardInService.save(p);
							}
						}
						//返回01为添加成功并返回
						result =  "01";
					/*if(pd.getString("SIZE_ARRAY")!=null&&pd.getString("SIZE_ARRAY")!=""){
						String json= gson.toJson(pd.get("SIZE_ARRAY"));
						PageData SIZE_object=gson.fromJson(json, PageData.class);
						//PageData SIZE_object = (PageData) pd.get("SIZE_ARRAY");
						PageData pro = procolorsizeInService.findById(SIZE_object);
						procolorsizeInService.edit(pro);
					}if (pd.getString("COLOR_ARRAY")!=null&&pd.getString("COLOR_ARRAY")!="") {
						String json= gson.toJson(pd.get("COLOR_ARRAY"));
						PageData COLOR_object=gson.fromJson(json, PageData.class);
						//PageData COLOR_object = (PageData) pd.get("COLOR_ARRAY");
						PageData pro = procolorsizeInService.findById(COLOR_object);
						procolorsizeInService.edit(pro);
					}if (pd.getString("STANDARD_ARRAY")!=null&&pd.getString("STANDARD_ARRAY")!="") {
						String json= gson.toJson(pd.get("STANDARD_ARRAY"));
						List<PageData> STANDARD_ARRAY = ToolsUtil.fromJson(json, PageData.class);
						for (PageData p : STANDARD_ARRAY) {
							standardInService.edit(p);
						}
					}*/
				//返回01为添加成功并返回
				result =  "01";
					}
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 显示商品所有的规格
	 * @Author zhaozhenhua
	 * @date   2018年1月29日
	 * @return
	 */
	@RequestMapping(value="/liststandard")
	@ResponseBody
	public Object listStandard(){
		logBefore(logger, "显示商品所有的规格");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("LISTSTANDARD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipDetails", pd)){	//检查参数
				List<PageData> list = standardInService.listAll(pd);
				map.put("list", list);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	
	/**
	 * 根据商家id获取商品列表
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年1月27日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getproductlistbyid")
	@ResponseBody
	public Object getProductListById(){
		logBefore(logger, "根据商家id获取商品列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPAll", pd.getString("FKEY"))){	//检验请求key值是否合法
				    List<PageData> list = productInService.getProductListById(pd);
				    if(!list.isEmpty()){
				      for (int i = 0; i < list.size(); i++) {
						if(list.get(i).getString("STATUS").equals("3")){
							list.remove(i);
						}
					  }
				    	for (PageData p : list) {
				    		switch (Integer.parseInt(p.getString("STATUS"))) {
				    		case 1:
				    			p.put("STATUS", "上架");
				    			break;
				    		case 2:
				    			p.put("STATUS", "下架");
				    			break;
				    		default:
				    			break;
				    		}
				    	}
				    	map.put("pd", list);
				    	//返回01为查询成功并返回
				    	result =  "01";
				    }else {
				    	result =  "02";
					}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据尺寸和颜色查询规格 
	 * @Author zhaozhenhua
	 * @date   2018年1月29日
	 * @return
	 */
	@RequestMapping(value="/getstandardbysize")
	@ResponseBody
	public Object getStandardBySize(){
		logBefore(logger, "根据尺寸和颜色查询规格 ");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETSTANDARDBYSIZE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getStandardBySize", pd)){	//检查参数
				PageData p = standardInService.getStandardBySize(pd);
				//返回01为添加成功并返回
				if(p==null){
					result="暂无此规格";
				}else{
					map.put("pd", p);
					result =  "01";
				}
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 根据规格展示商品
	 * @Author zhaozhenhua
	 * @date   2018年1月30日
	 * @return
	 */
    @CrossOrigin("http://inter.rongyaohk.com")
	@RequestMapping(value="/getprobystandard")
	@ResponseBody
	public Object getProByStandard(){
		logBefore(logger, "根据规格展示商品");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETPROBYSTANDARD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipDetails", pd)){	//检查参数
					pd.put("STANDARD_TYPE", "1");
					PageData SIZE_ARRAY = procolorsizeInService.listAll(pd);
					if(SIZE_ARRAY==null){
						result = "02";
						return AppUtil.returnObject(new PageData(), map);
					}
					ToolsUtil.putImgstoArray(SIZE_ARRAY,"NAME");
					map.put("SIZE_ARRAY", SIZE_ARRAY);
					
					PageData product = shipinService.getShipDetailsById(pd);
					if(product==null){
						result = "02";
						return AppUtil.returnObject(new PageData(), map);
					}
					PageData p = new PageData();
					p.put("PROPRICE", product.get("PROPRICE"));
					p.put("STOCK", product.get("STOCK"));
					p.put("PRODUCT_ID", product.get("PRODUCT_ID"));
					map.put("product", p);
					
					pd.put("STANDARD_TYPE", "2");
					PageData COLOR_ARRAY = procolorsizeInService.listAll(pd);
					if(COLOR_ARRAY==null){
						result = "02";
						return AppUtil.returnObject(new PageData(), map);
					}
					ToolsUtil.putImgstoArray(COLOR_ARRAY,"NAME");
					map.put("COLOR_ARRAY", COLOR_ARRAY);
					//返回01为添加成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据商品id获取规格
	 * @Author zhaozhenhua
	 * @date   2018年1月29日
	 * @return
	 */
	@RequestMapping(value="/getstandardbyid")
	@ResponseBody
	public Object getStandardById(){
		logBefore(logger, "根据商品id获取规格 ");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETSTANDARDBYID", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipDetails", pd)){	//检查参数
					pd.put("STANDARD_TYPE", "1");
					PageData SIZE_ARRAY = procolorsizeInService.listAll(pd);
					if(SIZE_ARRAY!=null){
						ToolsUtil.putImgstoArray(SIZE_ARRAY,"NAME");
						map.put("SIZE_ARRAY", SIZE_ARRAY);
						//返回01为添加成功并返回
						result =  "01";
					}
					pd.put("STANDARD_TYPE", "2");
					PageData COLOR_ARRAY = procolorsizeInService.listAll(pd);
					if(COLOR_ARRAY!=null){
						ToolsUtil.putImgstoArray(COLOR_ARRAY,"NAME");
						map.put("COLOR_ARRAY", COLOR_ARRAY);
					}
					List<PageData> STANDARD_ARRAY = standardInService.listAll(pd);
					if(STANDARD_ARRAY.size()!=0){
						map.put("STANDARD_ARRAY", STANDARD_ARRAY);
					}
					//返回01为添加成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据尺寸查询颜色 
	 * @Author zhaozhenhua
	 * @date   2018年1月29日
	 * @return
	 */
	@RequestMapping(value="/getcolorsbysize")
	@ResponseBody
	public Object getColorsBySize(){
		logBefore(logger, "根据尺寸查询颜色  ");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETCOLORSBYSIZE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getColorsBySize", pd)){	//检查参数
					List<PageData> list = standardInService.getColorsBySize(pd);
					//返回01为添加成功并返回
					if(list.size()!=0){
						map.put("list", list);
					}
					result = (list.size()==0)? "02" : "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 根据颜色查询尺寸 
	 * @Author zhaozhenhua
	 * @date   2018年1月29日
	 * @return
	 */
	@RequestMapping(value="/getsizesbycolor")
	@ResponseBody
	public Object getSizesByColor(){
		logBefore(logger, "根据颜色查询尺寸  ");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETSIZESBYCOLOR", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getSizesByColor", pd)){	//检查参数
					List<PageData> list = standardInService.getSizesByColor(pd);
					//返回01为添加成功并返回
					if(list.size()!=0){
						map.put("list", list);
					}
					result = (list.size()==0)? "02" : "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * app端根据颜色查询尺寸 
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年5月15日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getsizesbycolor2")
	@ResponseBody
	public Object getSizesByColor2(){
		logBefore(logger, "app端根据颜色查询尺寸  ");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETSIZESBYCOLOR2", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getSizesByColor", pd)){	//检查参数
					List<String> PRO_SIZE = new ArrayList<String>();
					List<PageData> list = standardInService.getSizesByColor(pd);
					for (PageData p : list) {
						PRO_SIZE.add(p.getString("PRO_SIZE"));
					}
					//返回01为添加成功并返回
					if(list.size()!=0){
						map.put("PRO_SIZE", PRO_SIZE);
					}
					result = (list.size()==0)? "02" : "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 后台获取品牌或者分类
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月1日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/getbrandorcate")
	@ResponseBody
	public Object getBrandOrCate(){
		logBefore(logger, "后台获取品牌或者分类 ");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("GETBRANDORCATE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipBrandIconList", pd)){	//检查参数
					List<PageData> list = shipinService.getBrandIconList(pd);
					//返回01为添加成功并返回
					if(list.size()!=0){
						map.put("list", list);
					}
					result = (list.size()==0)? "02" : "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 后台添加分类或品牌
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月1日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value="/addbrandorcate")
	@ResponseBody
	public Object addBrandOrCate(){
		logBefore(logger, "后台添加分类(品牌) ");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("ADDBRANDORCATE", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("addBrandOrCate", pd)){	//检查参数
					pd.put("BRANDICON_ID", this.getSnowId());
					shipinService.addBrandOrCate(pd);
					//返回01为添加成功并返回
					result = "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取推荐列表
	 * @author zhanglingbing
	 * @date 2018年3月12日
	 * @return
	 */
	@RequestMapping(value="/recommend")
	@ResponseBody
	public Object getRecommend(){
		logBefore(logger, "获取推荐列表");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("RECOMMEND", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("getRecommend", pd)){	//检查参数
					PageData productInfo = productInService.findById(pd);
					if(productInfo==null){
						result = "02";
						return AppUtil.returnObject(new PageData(), map);
					}
					pd.put("BRAND_ID", productInfo.get("BRAND_ID"));
					List<PageData> proList = productInService.getRecommendList(pd);
					if(proList==null||proList.size()==0){
						result = "02";
						return AppUtil.returnObject(new PageData(), map);
					}
					map.put("pd", proList);
					//返回01为添加成功并返回
					result = (proList.size()==0)? "02" : "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}


}
